package com.gitee.yangtianle.redisson.seckill.spring.boot.autoautoconfigure;


import com.gitee.yangtianle.redisson.seckill.config.RedissonMapProperties;
import com.gitee.yangtianle.redisson.seckill.interceptor.RedissonLimitInterceptor;
import com.gitee.yangtianle.redisson.seckill.interceptor.SingleCurrentLimitInterceptor;
import com.google.common.util.concurrent.RateLimiter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author tianle.ytl
 * @date 2018/9/19
 */
@Configuration
@ConditionalOnWebApplication
public class WebConfigInterceptors {

    @Configuration
    @ConditionalOnBean({RedissonClient.class, RateLimiter.class})
    @EnableConfigurationProperties(RedissonMapProperties.class)
    public class CurrentLimitingInterceptorConfigure implements WebMvcConfigurer {

        @Autowired
        private RedissonClient redissonClient;
        @Autowired
        private RateLimiter rateLimiter;
        @Autowired
        private RedissonMapProperties redissonMapProperties;

        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new RedissonLimitInterceptor(redissonClient, redissonMapProperties)).addPathPatterns("/**");
            registry.addInterceptor(new SingleCurrentLimitInterceptor(rateLimiter)).addPathPatterns("/**");
        }
    }
}
